package com.lottery.system.service.activitystatus.operater.impl;

import com.lottery.system.common.errorcode.ServiceErrorCodeConstants;
import com.lottery.system.common.exception.ServiceException;
import com.lottery.system.dao.dataobject.ActivityDO;
import com.lottery.system.dao.mapper.ActivityMapper;
import com.lottery.system.dao.mapper.ActivityPrizeMapper;
import com.lottery.system.service.activitystatus.enums.SequenceEnum;
import com.lottery.system.service.activitystatus.operater.ActivityStatusAbstractOperator;
import com.lottery.system.service.dto.ConvertActivityDTO;
import com.lottery.system.service.enums.ActivityPrizeStatusEnum;
import com.lottery.system.service.enums.ActivityStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: xiaoxie
 * create: 2024-09-12 20:06
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.service.activitystatus.operater
 * description: 活动状态扭转策略实现类
 */
@Component
public class ActivityStatusOperatorImpl implements ActivityStatusAbstractOperator {

    private final static Logger logger = LoggerFactory.getLogger(ActivityStatusOperatorImpl.class);
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
  /**
   * @description: 活动状态排序
   * @author: xiaoxie
   * @date: 2024/9/12 20:51
   * @param: []
   * @return: com.lottery.system.service.activitystatus.enums.SequenceEnum
   **/
    @Override
    public SequenceEnum sequence() {
        return SequenceEnum.AFTER;
    }

    /**
     * @description: 活动是否需要扭转策略
     * @author: xiaoxie
     * @date: 2024/9/12 20:09
     * @param: [convertActivityDTO]
     * @return: java.lang.Boolean
     **/
    @Override
    public Boolean needConvert(ConvertActivityDTO convertActivityDTO) {
        if(null == convertActivityDTO) {
            logger.warn(ServiceErrorCodeConstants.ACTIVITY_STATUS_ERROR.getMsg());
            throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_STATUS_ERROR);
        }
        //判断活动是否存在
        ActivityDO activityDO = activityMapper.selectActivityById(convertActivityDTO.getActivityId());
        if(null == activityDO) {
            logger.warn(ServiceErrorCodeConstants.ACTIVITY_DATA_IS_EMPTY.getMsg());
            throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_DATA_IS_EMPTY);
        }
        //判断活动的状态是否为COMPLETED
        if(activityDO.getStatus().equalsIgnoreCase(ActivityStatusEnum.COMPLETED.name())) {
            return false;
        }
        //判断奖品是否被全部抽取
        Integer count = activityPrizeMapper.activityPrizeCount(convertActivityDTO.getActivityId()
                                               ,ActivityPrizeStatusEnum.INIT.name());
        return count == 0;

    }

    /**
     * @description: 活动扭转状态是否成功的策略
     * @author: xiaoxie
     * @date: 2024/9/12 20:09
     * @param: [convertActivityDTO]
     * @return: java.lang.Boolean
     **/
    @Override
    public Boolean convertStatus(ConvertActivityDTO convertDTO) {
        if(null == convertDTO) {
            logger.warn(ServiceErrorCodeConstants.ACTIVITY_STATUS_ERROR.getMsg());
            throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_STATUS_ERROR);
        }
        try {
            activityMapper.updateStatusById(convertDTO.getActivityId()
                                           ,convertDTO.getActivityStatusTarget().name());
            return true;
        }catch (Exception e) {
            logger.warn("活动状态更新失败:",e);
            return false;
        }
    }
}
